<style lang="less">
</style>

<template>
    <div>
        <Card shadow>
            <Form :label-width="120">
                <Row gutter="10">
                    <Col span="4">
                        <FormItem label="起始时间：">
                            <DatePicker format="yyyy-MM-dd" type="date" placeholder="" @on-change="changeStartTime"></DatePicker>
                        </FormItem>
                    </Col>
                    <Col span="4">
                        <FormItem label="结束时间：">
                            <DatePicker format="yyyy-MM-dd" type="date" placeholder="" @on-change="changeEndTime"></DatePicker>
                        </FormItem>
                    </Col>
                    <Col span="4">
                        <FormItem label="查询医护：">
                            <Select v-model="condition.userId" clearable filterable style="width: 100%">
                                <Option v-for="item in users" :value="item.id" :key="item.id">{{ item.username }}</Option>
                            </Select>
                        </FormItem>
                    </Col>
                    <!--<Col span="4" align="left">-->
                        <!--<Button type="info" @click="search">查询</Button>-->
                    <!--</Col>-->
                </Row>
            </Form>
            <p>
                <Row>
                    <Col span="24">
                        <ButtonGroup shape="circle">
                            <Button type="ghost" v-for="(button,index) in buttons" v-if="button.parentId === menuId" :key="index" :icon="button.icon" @click="events(button)">
                                {{button.title}}
                            </Button>
                        </ButtonGroup>
                    </Col>
                </Row>
            </p>
            <iview-table
                ref="table"
                :baseURL="queryModel.baseURL"
                :menuId="dataModel.menuId"
                :columns="dataModel.columns"
                :order="dataModel.order"
                :pageNumber="pageModel.pageNumber"
                :pageSize="pageModel.pageSize"
                :defaultFilters="queryModel.defaultFilters"
                v-on="{getData:getData,changeSelect:changeSelect}">
            </iview-table>
            <p align="right">
                <Tag color="blue">诊所成交率：{{rate}}%</Tag>
            </p>
        </Card>
        <Modal v-model="showEdit" :closable='false' :mask-closable=true :width="700">
            <h3 slot="header" style="color:#2D8CF0">基本信息</h3>
            <Row>
                <Col span="22" offset="1">
                    <Form ref="formItem" :model="formItem" :label-width="140">
                        <FormItem label="新诊人数：" prop="categoryId">
                            <Input style="width: 100%" v-model="formItem.newNumber"></Input>
                        </FormItem>
                        <FormItem label="新诊单次成交人次：" prop="categoryId">
                            <Input style="width: 100%" v-model="formItem.newSingleTransaction"></Input>
                        </FormItem>
                        <FormItem label="新诊单次成交金额：" prop="categoryId">
                            <Input style="width: 100%" v-model="formItem.newSingleAmount"></Input>
                        </FormItem>
                        <FormItem label="新诊疗程成交人次：" prop="categoryId">
                            <Input style="width: 100%" v-model="formItem.newCourseTransaction"></Input>
                        </FormItem>
                        <FormItem label="新诊疗程成交金额：" prop="categoryId">
                            <Input style="width: 100%" v-model="formItem.newCourseAmount"></Input>
                        </FormItem>
                        <FormItem label="新诊缴费总金额：" prop="categoryId">
                            <Input style="width: 100%" v-model="formItem.newPayTotalAmount"></Input>
                        </FormItem>
                        <FormItem label="复诊单次再消费人次：" prop="categoryId">
                            <Input style="width: 100%" v-model="formItem.reviewSingleTransaction"></Input>
                        </FormItem>
                        <FormItem label="复诊单次再消费金额：" prop="categoryId">
                            <Input style="width: 100%" v-model="formItem.reviewSingleReamount"></Input>
                        </FormItem>
                        <FormItem label="复诊续疗程人次：" prop="categoryId">
                            <Input style="width: 100%" v-model="formItem.reviewRenewalCourseTransaction"></Input>
                        </FormItem>
                        <FormItem label="复诊续疗程金额：" prop="categoryId">
                            <Input style="width: 100%" v-model="formItem.reviewRenewalCourseAmount"></Input>
                        </FormItem>
                        <FormItem label="治疗人次：" prop="categoryId">
                            <Input style="width: 100%" v-model="formItem.treatTransaction"></Input>
                        </FormItem>
                        <FormItem label="划扣收入：" prop="categoryId">
                            <Input style="width: 100%" v-model="formItem.deductAmount"></Input>
                        </FormItem>
                    </Form>
                </Col>
            </Row>
            <div slot="footer">
                <Button type="primary" @click="save">确认</Button>
            </div>
        </Modal>
    </div>
</template>

<script>
import iviewTable from '@/view/common/table/Table.vue';
import iviewPage from '@/view/common/page/Page.vue';
export default {
    name: 'text-editor',
    components: {
        iviewTable,
        iviewPage
    },
    data () {
        return {
            menuId: 170,
            showEdit: false,
            buttons: [],
            selectRows: [],
            baseURL: '/user/statistics/financial',
            queryModel: {
                // 网关代理路径(必须)+资源路径(可选)
                baseURL: '/user/statistics/financial',
                defaultFilters: [

                ]
            },
            dataModel: {
                menuId: -1,
                columns: [
                    {
                        type: 'selection',
                        width: 60,
                        align: 'center'
                    },
                    {
                        title: '姓名',
                        key: 'userName',
                        sortable: true
                    },{
                        title: '新诊人数',
                        key: 'newNumber',
                        sortable: true
                    },{
                        title: '新诊单次成交人次',
                        key: 'newSingleTransaction',
                        sortable: true
                    },{
                        title: '新诊单次成交金额',
                        key: 'newSingleAmount',
                        sortable: true,
                        render: (h, params) => {
                            let temp = 0
                            if (params.row.newSingleAmount) {
                                temp = params.row.newSingleAmount
                            }
                            return h('span', temp)
                        }
                    },{
                        title: '新诊疗程成交人次',
                        key: 'newCourseTransaction',
                        sortable: true
                    },{
                        title: '新诊疗程成交金额',
                        key: 'newCourseAmount',
                        sortable: true,
                        render: (h, params) => {
                            let temp = 0
                            if (params.row.newCourseAmount) {
                                temp = params.row.newCourseAmount
                            }
                            return h('span', temp)
                        }
                    },{
                        title: '新诊缴费总金额',
                        sortable: true,
                        render: (h, params) => {
                            let temp = 0
                            if (params.row.newSingleAmount) {
                                temp += params.row.newSingleAmount
                            }
                            if (params.row.newCourseAmount) {
                                temp += params.row.newCourseAmount
                            }
                            return h('span',  temp)
                        }
                    },{
                        title: '复诊单次再消费人次',
                        key: 'reviewSingleTransaction',
                        sortable: true
                    },{
                        title: '复诊单次再消费金额',
                        key: 'reviewSingleReamount',
                        sortable: true,
                        render: (h, params) => {
                            let temp = 0
                            if (params.row.reviewSingleReamount) {
                                temp = params.row.reviewSingleReamount
                            }
                            return h('span', temp)
                        }
                    },{
                        title: '复诊续疗程人次',
                        key: 'reviewRenewalCourseTransaction',
                        sortable: true
                    },{
                        title: '复诊续疗程金额',
                        key: 'reviewRenewalCourseAmount',
                        sortable: true,
                        render: (h, params) => {
                            let temp = 0
                            if (params.row.reviewRenewalCourseAmount) {
                                temp = params.row.reviewRenewalCourseAmount
                            }
                            return h('span', temp)
                        }
                    },{
                        title: '治疗人次',
                        key: 'treatTransaction',
                        sortable: true
                    },{
                        title: '划扣收入',
                        key: 'deductAmount',
                        sortable: true,
                        render: (h, params) => {
                            let temp = 0
                            if (params.row.deductAmount) {
                                temp = params.row.deductAmount
                            }
                            return h('span', temp)
                        }
                    },
                    {
                        title: '个人成交率',
                        sortable: true,
                        render: (h, params) => {
                            let temp = 0
                            let temp1 = 0
                            let temp2 = 0
                            if (params.row.newSingleTransaction) {
                                temp1 += params.row.newSingleTransaction
                            }
                            if (params.row.newCourseTransaction) {
                                temp1 += params.row.newCourseTransaction
                            }
                            if (params.row.newNumber) {
                                temp2 = params.row.newNumber
                            }
                            if (temp2 > 0) {
                                temp = temp1 / temp2
                            }
                            return h('span', temp + '%')
                        }
                    }
                ],
                order: 'id desc'
            },
            pageModel: {
                totalSize: 0,
                pageNumber: 1,
                pageSize: 10,
                pageSizeOpts: [10, 50, 100, 500]
            },
            formItem: {
                newNumber: '',
                newCourseTransaction: '',
                newCourseAmount: '',
                newSingleTransaction: '',
                newSingleAmount: '',
                newPayTotalAmount: '',
                reviewSingleTransaction: '',
                reviewSingleReamount: '',
                reviewRenewalCourseTransaction: '',
                reviewRenewalCourseAmount: '',
                treatTransaction: '',
                deductAmount: ''
            },
            condition: {
                userId: '',
                startTime: '',
                endTime: ''
            },
            users: [],
            rate: 0
        };
    },
    methods: {
        events (item) {
            let path = item.path
            if (path === 'edit') {
                this.edit()
            }
        },
        edit () {
            if (this.selectRows.length === 1) {
                let row = this.selectRows[0]
                if (row.id && row.id !== '') {
                    this.formItem = row
                    this.showEdit = true
                } else {
                    this.$bus.modal('warning', '系统提醒', '只能修改某一天的财务数据,合并查询时无法修改')
                }
            } else {
                this.$bus.alert('warning', '请选择一条记录')
            }
        },
        save () {
            let url = this.baseURL + '/saveOrUpdate'
            this.$ajax.post(url, this.formItem).then(r => {
                let {code, msg} = r.data
                if (code === 0) {
                    this.$bus.alert('success', '操作成功')
                    this.showEdit = false
                    this.refresh()
                } else {
                    this.$bus.alert('error', msg)
                }
            })
        },
        getData (datas) {
            let temp1 = 0
            let temp2 = 0
            for (let data of datas) {
                if (data.newSingleTransaction) {
                    temp1 += data.newSingleTransaction
                }
                if (data.newCourseTransaction) {
                    temp1 += data.newCourseTransaction
                }
                if (data.newNumber) {
                    temp2 += data.newNumber
                }
            }
            if (temp2 > 0) {
                this.rate = temp1 / temp2
            }
        },
        loadButton () {
            this.$ajax.get('/user/buttons/' + this.menuId).then((response) => {
                let {code, data, msg} = response.data
                if (code === 0) {
                    this.buttons = data
                } else {
                    this.$bus.alert('error', msg)
                }
            })
        },
        loadUsers () {
            let url = '/user/loadUser'
            this.$ajax.get(url).then(r => {
                let {code, data, msg} = r.data
                if (code === 0) {
                    this.users = data
                } else {
                    this.$bus.alert('error', msg)
                }
            })
        },
        search () {
            this.queryModel.defaultFilters.length = 0
            if (this.condition.userId !== '') {
                this.queryModel.defaultFilters.push({'field': 'user_id', 'value1': this.condition.userId, 'operate': '=='})
            }
            if (this.condition.startTime !== '') {
                this.queryModel.defaultFilters.push({'field': 'creation_time', 'value1': this.condition.startTime + ' 00:00:00', 'operate': '>='})
            }
            if (this.condition.endTime !== '') {
                this.queryModel.defaultFilters.push({'field': 'creation_time', 'value1': this.condition.endTime + ' 23:59:59', 'operate': '<='})
            }
            this.refresh()
        },
        refresh () {
            if (this.condition.startTime === '') {
                this.$bus.alert('warning', '请选择查询开始时间')
                return
            }
            if (this.condition.endTime === '') {
                this.$bus.alert('warning', '请选择查询结束时间')
                return
            }
            this.$refs.table.refresh()
        },
        changeStartTime (val, t) {
            this.condition.startTime = val
        },
        changeEndTime (val, t) {
            this.condition.endTime = val
        },
        changeSelect (selection) {
            this.selectRows = selection
        },
        init () {
            this.refresh()
        }
    },
    mounted () {
        // 加载按钮组
        this.loadButton()
        // 加载医护
        this.loadUsers()
        // 初始化
        // this.init()
    },
    watch: {
        '$route' (to) {
            if (to.path === '/statistics/finance') {
                this.init()
            }
        },
        'condition.userId': function (val, oldValue) {
            this.search()
        },
        'condition.startTime': function (val, oldValue) {
            this.search()
        },
        'condition.endTime': function (val, oldValue) {
            this.search()
        }
    }
}
</script>

<style>

</style>
